package com.example.springsecuritydemo.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间类型的数据处理方法汇总
 *
 * @author Administrator
 */
public class TimeUtils {

    /**
     * 将CreateTime转换成标准格式的时间（yyyy-MM-dd HH:mm:ss）
     */
    public static String formatTime(String createTime) {
        long msgCreateTime = Long.parseLong(createTime) * 1000L;
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(new Date(msgCreateTime));
    }

    /**
     * 获取当前时间并转换为CreateTime
     *
     * @param args
     */
    public static long getCurrentTime() {
        return System.currentTimeMillis();
    }

    /**
     * 把date类型转换成特定格式的String类型
     *
     * @param date
     * @param partten
     * @return String
     */
    public static String dateToString(Date date, String partten) {
        String result = null;
        String timezoneID = "GMT+8:00";
        SimpleDateFormat formatter = new SimpleDateFormat(partten, new java.util.Locale("zh", "CN"));
        java.util.TimeZone timezone = java.util.TimeZone.getTimeZone((String) timezoneID);
        Calendar calendar = new java.util.GregorianCalendar(timezone);
        formatter.setCalendar(calendar);
        result = formatter.format(date);
        return result;
    }

    /**
     * 把Date类型转换成String类型
     *
     * @param param
     * @return 返回的字符串,如果参数是空则返回空
     */
    public static String dateFormat(Date param) {
        if(param == null )
            return  "";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");
        return simpleDateFormat.format(param);
    }

    /**
     * 将字符串转化为日期类型-yyyy-mm-dd
     *
     * @param time
     * @return Date
     * @throws ParseException
     */
    public static Date stringFormatDate(String s) {
        if ("".equals(s) || null == s) {
            return null;
        }
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            return simpleDateFormat.parse(s);
        } catch (ParseException pe) {
            return null;
        }
    }

    /**
     * 将字符串转化为日期类型-yyyy-mm-dd HH24:mm:ss
     *
     * @param time
     * @return Date
     * @throws ParseException
     */
    public static Date formatToDate(String s) {
        Date date = null;
        if (s == null) {
            return null;
        } else {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                date = sdf.parse(s);
                return new Date(date.getTime());
            } catch (ParseException ex) {
                return null;
            }
        }
    }

    /**
     * 得到当前时间的整点
     *
     * @return
     */
    @SuppressWarnings("deprecation")
    public static String getWholePoint() {
        Calendar calendar = Calendar.getInstance();
        Date date = calendar.getTime();
        return date.getHours() + "";
    }

    public static String formatTimeCn(String ss) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日HH点mm分ss秒");
        Date date = null;
        if (ss == null) {
            return "";
        } else {
            try {
                date = sdf.parse(ss);
            } catch (ParseException e) {
                date = new Date();
            }
        }
        return sdf2.format(date);
    }

    public static String formatTimeDate(String ss) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日");
        Date date = null;
        if (ss == null) {
            return "";
        } else {
            try {
                date = sdf.parse(ss);
            } catch (Exception e) {
                date = new Date();
            }
        }
        return sdf2.format(date);
    }

    public static String formatTimeHm(String ss) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm");

        Date date = null;
        if (ss == null) {
            return "";
        } else {
            try {
                date = sdf.parse(ss);
            } catch (Exception e) {
                date = new Date();
            }
        }
        if (sdf2.format(date) != null) {
            return sdf2.format(date);
        } else {
            return "00:00";
        }

    }


    public static String formatWeek(String ss) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf2 = new SimpleDateFormat("EEEE");
        Date date = null;
        if (ss == null) {
            return "";
        } else {
            try {
                date = sdf.parse(ss);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                date = new Date();
            }
        }
        return sdf2.format(date);
    }

    public static Date getNowDate() {
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            //创建时间
            return dateFormat.parse(dateFormat.format(new Date()));
        } catch (ParseException e) {
            e.printStackTrace();
            throw new RuntimeException("获取日期时间出错");
        }
    }
}
